package com.woniuxy.vuecli.controller;

import com.woniuxy.vuecli.entity.Menu;
import com.woniuxy.vuecli.entity.ResponseResult;
import com.woniuxy.vuecli.service.MenuService;
import com.woniuxy.vuecli.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author author
 * @Create 2024-01-15 10:09
 */
//@CrossOrigin
@Slf4j
@RequestMapping("/menu")
@RestController
public class MenuController {

    @Autowired
    MenuService menuService;


    @RequestMapping("/getAll")
    public ResponseResult<List<Menu>> getAll(){
        return new ResponseResult<>(200,"ok", menuService.getAll());
    }

    @RequestMapping("/remove/{mid}")
    public ResponseResult<String> remove(@PathVariable("mid") int mid){
        menuService.removeByMid(mid);
        return new ResponseResult<>(200,"ok", "");
    }

    @RequestMapping("/getMidsByRid/{rid}")
    public ResponseResult<List<Integer>> getMidsByRid(@PathVariable("rid")int rid){
        return new ResponseResult<List<Integer>>(200,"ok", menuService.getMidsByRid(rid));
    }

    @RequestMapping("/getByEid")
    public ResponseResult<List<Menu>> getByEid(@RequestHeader("token")String token ){
        log.info("token={}",token);
        return new ResponseResult<>(200,"ok",
                menuService.getByEid(Integer.parseInt(JwtUtil.getEid(token))));
    }


}
